<template>
  <div class="chat-list">
    <van-nav-bar left-arrow @click-left="goBack"/>
    <div class="title">Conversas</div>
    <UnLogin v-if="!hasLogin"></UnLogin>
    <div v-else class="chat-list-content">
      <van-list
          v-model="loading"
          :finished="finished"
          :finished-text="text"
          @load="onLoad" >
        <div class="chat-list-item" v-for="(item,index) in list" :key="index" @click="toChat(item)">
          <div class="left-img">
            <van-image v-if="item.askImg&&item.askImg.startsWith('http')" :src="item.askImg" width="60px" height="60px" fit="cover" round />
            <van-image v-else :src="domain + item.askImg" width="60px" height="60px" fit="cover" round />
            <!-- <van-image
                round
                width="60px"
                height="60px"
                fit="cover"
                :src="domain + item.askImg"
            /> -->
          </div>
          <div class="right-person">
            <div class="chat-list-detail">
              <div class="name">{{item.supplyName}}</div>
              <div class="leave-word">{{item.latestContent}}</div>
            </div>
          </div>
        </div>
      </van-list>
      
    </div>
  </div>
</template>
<script>
import { isLogin } from "@/utils";
import Api from "@/api";
import UnLogin from "@/components/unlogin";
import { Domain } from "@/constants";
export default {
  name: 'Chat',
  components: { UnLogin },
  data() {
    return {
      hasLogin: false,
      list:[],
      loading: false,
      finished: false,
      domain: Domain,
      params:{
        pageNo:0,
        pageSize:20
      },
      text:""
    };
  },
  created() {
    this.hasLogin = isLogin();
  },
  methods:{
    toChat(val){
      this.$router.push({path:"chatDetail",query:{detail:JSON.stringify(val)}})
    },
    getList(){
      Api.sendUsers(this.params).then(res=>{
        let arr = res.records
        if (this.params.pageSize * this.params.pageNo >= res.total) {
          this.loading = false;
          this.finished = true;
        }
        if (this.params.pageNo > 1) {
          this.list = this.list.concat(arr);
        } else {
          this.list = arr;
        }
      })
    },
     onLoad() {
      this.params.pageNo++
      this.getList()
    },
    goBack(){
      this.$router.go(-1)
    },
  }
};
</script>
<style scoped lang="scss">
::v-deep.van-nav-bar{
  background-color: #fff;
  padding: 0 10px;
  .van-nav-bar__left{
    padding: 0;
    .van-icon{
      font-size: 26px;
      color: #000000;
    }
  }
}
.title {
  font-size: 20px;
  line-height: 50px;
  padding-left: 16px;
  font-weight: bold;
  background: #fff;
}
::v-deep.van-row{
  background-color: #fff;
  .van-col{
    padding: 10px;
  }
  .van-col--6{
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .van-col--18{
    border-bottom: 1px solid #f5f5f5;
  }
}
.chat-list{
  height: 100%;
}

.chat-list-content{
  height: 100%;
  border-top: 1px solid #f2f2f2;
  .van-list{
    height: 100%;
    padding-bottom: 20px;
  }
}
.chat-list-item{
  display: flex;
  background-color: #fff;
  .left-img{
    height: 90px;
    width: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .right-person{
    height: 90px;
    border-bottom: 1px solid #f2f2f2;
  }
}
.chat-list-detail{
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-right: 10px;
  height: 100%;
  .name{
    letter-spacing: 1px;
    font-size: 14px;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
  }
  .leave-word{
    margin-top: 10px;
    font-size: 12px;
    color: #999;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
  }
}
</style>
